package indi.twj.mybluetool.activity;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import java.util.UUID;

import indi.twj.mybluetool.R;

/**
 * lanya
 * Created by Administrator on 2017/11/6.
 */
public class DeviceInfoActivity extends AppCompatActivity {

    public static final UUID SERVICE_UUID = UUID.fromString("a368d08c-e68c-5ae5-bd27-d95c5f98a6cf");
    public static final UUID CHARACTERISTIC_UUID = UUID.fromString("0f9c59cf-5631-5b60-82ae-1524113a7e61");

    private BluetoothGatt bluetoothGatt;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.activity_device_info);

        initView();

    }

    @Override
    protected void onStart() {
        connect(getIntent().getIntExtra("no", 0));
        super.onStart();
    }

    /**
     * connect to device
     *
     * @param i
     */
    private void connect(int i) {
        //connect bluetooth
        //.....
        BluetoothDevice blueDevice = DeviceListActivity.deviceList.get(i);
        bluetoothGatt = blueDevice.connectGatt(this, true, new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                super.onConnectionStateChange(gatt, status, newState);

                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    gatt.discoverServices();   //连接成功，开始搜索服务，一定要调用此方法，否则获取不到服务
                }
            }

            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
            }

            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicRead(gatt, characteristic, status);
            }

            @Override
            public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                super.onDescriptorWrite(gatt, descriptor, status);
            }
        });
    }

    private void initView() {

    }

    private void send(byte[] value) {
        BluetoothGattService service = bluetoothGatt.getService(SERVICE_UUID);
        if (service != null) {
            BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID);
            if (characteristic != null) {
                bluetoothGatt.setCharacteristicNotification(characteristic, true);  //设置characteristic的通知，触发bluetoothGatt.onCharacteristicWrite()事件。

                // write
                characteristic.setValue(value);
                bluetoothGatt.writeCharacteristic(characteristic);
            }
        }
    }
}
